/*
 * @Author: your name
 * @Date: 2021-09-15 10:19:03
 * @LastEditTime: 2022-11-15 08:23:42
 * @LastEditors: zshawk1982
 * @Description: In User Settings Edit
 * @FilePath: \media_system\database\index.js
 */
const { Sequelize, DataTypes } = require("sequelize");
const config = {
  host: "localhost", //主机名
  database: "sugar_system", //使用的哪个数据库名
  username: "zshawk", //账号
  password: "root", //密码
  port: 3306, //端口号，mysql默认3306
};
const sequelize = new Sequelize(
  config.database,
  config.username,
  config.password,
  {
    host: config.host, //数据库地址
    port:config.port,
    dialect: "mysql", //指定连接的数据库类型
    dialectOptions: { charset: "utf8mb4"},
    'define': {
      'underscored': true,//字段下划线
      'charset':'utf8mb4'//必须设置了才能保存中文
  },
    pool: {
      max: 5, //连接池最大连接数量
      min: 0, //最小连接数量
      idle: 10000, //如果一个线程 10秒内么有被使用过的话，就释放
    },
    // logging: true, // 执行过程会log一些SQL的logging，设为false不显示,但不要设置为true，反而会报错，最好选择不设置使用默认值
    timezone: '+08:00' //东八时区
  }
);

const User = require("./models/user")(sequelize, DataTypes);
const Knowledge = require("./models/knowledge")(sequelize, DataTypes);
const Food = require("./models/food")(sequelize, DataTypes);
const DietRecord = require("./models/dietRecord")(sequelize, DataTypes);
const DietRecordDetail = require("./models/dietRecordDetail")(sequelize, DataTypes);
const Recipe = require("./models/recipe")(sequelize, DataTypes);
const RecipeDetail = require("./models/recipeDetail")(sequelize, DataTypes);
const Bloodglucose = require("./models/bloodglucose")(sequelize, DataTypes);
const Hemoglobin = require("./models/hemoglobin")(sequelize, DataTypes);
const RecommendRecipe = require("./models/recommendRecipe")(sequelize, DataTypes);

//用户和饮食记录是一对多关系，DietRecord表有个外键userId指向user表的主键userId
User.hasMany(DietRecord, {
  foreignKey: {
    name: "userId",//DietRecord.userId
  },
  onDelete:'CASCADE'
});
DietRecord.belongsTo(User, {
  foreignKey: "userId",//DietRecord.userId
  targetKey: "userId",//User.userId
});

//用户和血糖记录是一对多关系，Bloodglucose表有个外键userId指向user表的主键userId
User.hasMany(Bloodglucose, {
  foreignKey: {
    name: "userId",//Bloodglucose.userId
  },
  onDelete:'CASCADE'
});
Bloodglucose.belongsTo(User, {
  foreignKey: "userId",//Bloodglucose.userId
  targetKey: "userId",//User.userId
});
//用户和糖化血红白是一对多关系，Hemoglobin表有个外键userId指向user表的主键userId
User.hasMany(Hemoglobin, {
  foreignKey: {
    name: "userId",//Hemoglobin.userId
  },
  onDelete:'CASCADE'
});
Hemoglobin.belongsTo(User, {
  foreignKey: "userId",//Hemoglobin.userId
  targetKey: "userId",//User.userId
});

//饮食记录和食物是多对多关系
Food.belongsToMany(DietRecord, { through: DietRecordDetail, foreignKey: 'foodId', otherKey: 'dietRecordId' })
DietRecord.belongsToMany(Food, { through: DietRecordDetail, foreignKey: 'dietRecordId', otherKey: 'foodId',as: 'foods' })

//食谱和食物是多对多关系
Food.belongsToMany(Recipe, { through: RecipeDetail, foreignKey: 'foodId', otherKey: 'recipeId' })
Recipe.belongsToMany(Food, { through: RecipeDetail, foreignKey: 'recipeId', otherKey: 'foodId',as: 'foods' })

module.exports = { sequelize, User, Knowledge, Food,DietRecord,DietRecordDetail,Bloodglucose,Hemoglobin,Recipe,RecipeDetail,RecommendRecipe };
